#### TABLE B.5: ATTRITION
#### Study 3 Wave 2 attrition analysis

rm(list = ls())
source("./2_code/00_setup.R")

#### LOAD DATA ####

data3 <- fread(paste0(data_path, "data_study3.csv"), header = TRUE)
data3b <- fread(paste0(data_path, "data_study3_wave2.csv"), header = TRUE)


#### IDENTIFY WAVE 2 RESPONDENTS ####

data3b_presence <- data3b %>%
  distinct(idofferwise) %>%
  mutate(answered_wave2 = 1)

data3 <- data3 %>%
  left_join(data3b_presence, by = "idofferwise") %>%
  mutate(answered_wave2 = ifelse(is.na(answered_wave2), 0, answered_wave2))


#### ATTRITION REGRESSION ####

attrition <- lm(answered_wave2 ~ treat_economic + treat_humanitarian, data = data3)
summary(attrition)

model_summary <- summary(attrition)
coef_df <- as.data.frame(model_summary$coefficients)
colnames(coef_df) <- c("Estimate", "Std. Error", "t value", "p-Value")

# Remove the intercept
coef_df <- coef_df[-1, ]

# Rename coefficients
rownames(coef_df) <- c("Economic Treatment", "Humanitarian Treatment")


#### EXPORT TO LATEX ####

latex_table <- xtable(coef_df)

latex_output <- print(
  latex_table,
  type = "latex",
  include.rownames = TRUE,
  print.results = FALSE
)

writeLines(latex_output, paste0(tables_path, "table_B5.tex"))
